package study.queue;

import java.util.Scanner;

public class ArrayQueue {
    private int maxSize;    //队列最大容量
    private int rear;       //队尾
    private int front;      //队头
    private int[] arr;      //存放队列数据，用数组模拟队列

    //队列构造器
    public ArrayQueue(int arrMaxSize){
        this.maxSize = arrMaxSize;
        //指向队尾，rear指向的是队列最后一个数据
        this.rear = -1;
        //指向对头，front指向的是对头的前一个位置
        this.front = -1;
        this.arr = new int[arrMaxSize];
    }

    //判断队列是否满
    public Boolean isFull(){
        return rear == maxSize - 1;
    }

    //判断队列是否空
    public Boolean isEmpty(){
        return rear == front;
    }


    //进队
    public void enQueue(int data){
        if (isFull()){
            System.out.println("队列已满，不能加入数据");
            return;
        }
        rear++; //尾部+1
        arr[rear] = data;   //将数据存入队列
    }


    //出队
    public int deQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空，没有数据了");
        }
        front++;
        return arr[front];

    }

    //清空队列
    public void clearQueue(int arrMaxSize){
        arr = new int[arrMaxSize];
        this.maxSize = arrMaxSize;
        this.front = -1;
        this.rear = -1;
    }

    //显示队列
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空，没有数据。");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d] = %d\n", i, arr[i]);
        }

    }

    //取出对头数据
    public int getFront(){
        if (isEmpty()){
            throw new RuntimeException("队列为空，没有数据。");
        }
        return arr[front + 1];
    }

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        Scanner sc = new Scanner(System.in);
        char key = ' ';
        Boolean loop = true;

        while (loop){
            System.out.println("============== 菜单 ==============");
            System.out.println("e（enQueue）：进队");
            System.out.println("d（deQueue）：出队");
            System.out.println("f（getFront）：查看对头");
            System.out.println("s（showQueue）：显示队列");
            System.out.println("c（clearQueue）：清除队列");
            System.out.println("============================");
            System.out.print("请输入字母选择菜单：");
            key = sc.next().charAt(0);
            switch (key){
                case 'e':
                    System.out.print("请输入进队的数据：");
                    int data = sc.nextInt();
                    queue.enQueue(data);
                    break;
                case 'd':

                    try {
                        int deQueueData = queue.deQueue();
                        System.out.println("出队的数据是：" + deQueueData);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'f':
                    try {
                        int front = queue.getFront();
                        System.out.println("当前对头数据：" + front);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 's':
                    System.out.println("显示队列：");
                    queue.showQueue();
                    break;
                case 'c':
                    queue.clearQueue(3);
                    break;
                default:
                    System.out.println("请输入正确的指令！");
                    break;
            }
        }

        sc.close();


    }
}
